﻿--[[
	Copyright (c) 2012 Eyal Shilony

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]

local addonName, addon = ...
local dropdown = CreateFrame("Frame", "LonnyMenuFrame", nil, "UIDropDownMenuTemplate")
local commando = LibStub("Commando-1.0")
local L = LibStub("AceLocale-3.0"):GetLocale(addonName)

local menu = { }

local function insertItem(name, order)
	local action = commando:GetAction(name)
	if action and order > 0 then
		table.insert(menu, {
			order = order,
			name = name,
			text = action.title,
			icon = action.icon,
			notCheckable = 1,
			func = function() commando:RunAction(name) end,
		})
	end
end

local function insertSeparator()
	if #menu > 0 then
		table.insert(menu, {
			text = "",
			notCheckable = 1,
			disabled = true,
		})
	end
end

local function comp(a, b)
	return a.order < b.order
end

function addon:BuildMenu()
	wipe(menu)
	
	local order = 0
	for name, order in pairs(self.db.profile.order) do
		insertItem(name, order)
	end
	table.sort(menu, comp)
	
	if not self.db.profile.hideLogoutButtons then
		insertSeparator()
		table.insert(menu, {
			text = LOGOUT,
			notCheckable = 1,
			func = function()
				Logout()
				HideUIPanel(GameMenuFrame)
			end,
		})
	end
	
	if not self.db.profile.hideLogoutButtons then
		table.insert(menu, {
			text = EXIT_GAME,
			notCheckable = 1,
			func = function() 
				Quit()
				HideUIPanel(GameMenuFrame)
			end,
		})
	end
	
	if not self.db.profile.hideReloadUIButton then
		insertSeparator()
		table.insert(menu, {
			text = L["Reload UI"],
			notCheckable = 1,
			func = function() 
				ReloadUI()
			end,
		})
	end
	
	if not self.db.profile.hideOptionsButton then
		insertSeparator()
		table.insert(menu, {
			text = L["Options"],
			notCheckable = 1,
			func = function()
				if not InterfaceOptionsFrame:IsVisible() then
					HideUIPanel(GameMenuFrame)
					InterfaceOptionsFrame_OpenToCategory(addon.name)
				else
					HideUIPanel(InterfaceOptionsFrame)
				end
			end,
		})
	end
	
	LonnyMenuFrame:Hide()
end

function addon:Open(frame, button)
	local name
	local isLeftButton = self.db.profile.mouseButton == 1 and button == "LeftButton"
	local isRightButton = self.db.profile.mouseButton == 2 and button == "RightButton"
	
	for i, v in pairs(self.db.profile.accessibility) do
		if not IsModifierKeyDown() and v.modifier == 4
		or IsAltKeyDown() and v.modifier == 1 
		or IsControlKeyDown() and v.modifier == 2 
		or IsShiftKeyDown() and v.modifier == 3 then
			name = v.name
			break
		end
	end
	
	if isLeftButton or isRightButton then
		commando:RunAction(name)
	else
		EasyMenu(menu, dropdown, frame, 0, 0, "MENU")
	end
end